
package ejemplodeawt;
import java.awt.*;
import java.awt.event.*;

public class xy extends Canvas {

    private TextField x;
    private TextField y;
    private int x1;
    private int x2;
    private int y1;
    private int y2;

    private int clicks;
     public void paint(Graphics g) {

         addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                clicks=e.getClickCount();
                if(clicks==1){
                    System.out.println("un click");
                x1=new Integer(e.getX());
                y1=new Integer(e.getY());
                }else{
                if(clicks==2){
                    System.out.println("dos clicks");
                x2=new Integer(e.getX());
                y2=new Integer(e.getY());
                }
            }}
        });
            g.setColor(Color.red);
            g.drawLine(x1, y1, x2, y2);
        }
    public void ventana() {
        Frame f = new Frame();

        Panel p = new Panel();
        p.setBackground(Color.red);

        xy Dibujo = new xy();
        f.add(Dibujo);
        Dibujo.setBackground(Color.black);
        Dibujo.repaint();
        f.add(BorderLayout.SOUTH, p);
        x = new TextField("            ");
        y = new TextField("            ");
        Label xl = new Label("X =");
        Label yl = new Label("Y =");
        p.add(xl);
        p.add(x);
        p.add(yl);
        p.add(y);
        //Dibujo.
        Dibujo.addMouseMotionListener(new MouseMotionAdapter() {

            public void mouseMoved(MouseEvent e) {
                Float a = new Float(e.getX());
                Float b = new Float(e.getY());
                x.setText(String.valueOf(a));
                y.setText(String.valueOf(b));
            }
        });
        f.setBounds(50, 50, 500, 500);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
    }

    

    public static void main(String[] args) {
        xy v = new xy();
        v.ventana();
    }
}

